//
//  WYStatusViewModel.swift
//  weibo
//
//  Created by 王玉东 on 16/4/13.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit

class WYStatusViewModel: NSObject {
    
   
// lazy var statuess:[WYUserStatues] = [WYUserStatues]()
    lazy var statuesArray:[WYStatusModel] = [WYStatusModel]()
//func loadPageListDate(finish:(isSucces:Bool) -> ()){
    func loadPageListDate(isPullup: Bool,finish:(isSucces:Bool) -> ()){
    
        guard let token = WYUserAccaseViewModel.sharedViewModel.userAccout?.access_token else{
            //用户没有登录
            return
        }
        var max_id: Int64 = 0
        var since_id: Int64 = 0
        if isPullup {
        max_id = statuesArray.first?.status?.id ?? 0
        }else{
        since_id = statuesArray.last?.status?.id ?? 0
        }
    //  let max_id = statuesArray.last?.status?.id ?? 0
        
    HMNetworkTools.sharedTools.loadHomePageDate(token,max_id: max_id,since_id: since_id) { (result, error) -> () in
            
            if error != nil {
                //数据错误
                return
            }
            guard let dic = result as? [String : AnyObject] else{
                //数据转化错误
                return
            }
            //这里是数组[[String :AnyObject]]
            guard let Array = dic["statuses"] as? [[String :AnyObject]] else {
                //输出的格式不正确
                return
            }
            
            //var temArray = [WYUserStatues]()
            var temArray = [WYStatusModel]()
            for dict in Array{
                
                let obj = WYUserStatues.init(dic: dict)
                
                let viewModel = WYStatusModel(status: obj)
                
                temArray.append(viewModel)
            }
        

        if isPullup {
        self.statuesArray = self.statuesArray + temArray
        }else{
        self.statuesArray = temArray + self.statuesArray
        }
            
            finish(isSucces: true)
            //刷新数据
           // self.tableView.reloadData()
        }
    
    
    
    }
    

}
